"""
A class to exercise the JT core functions.
"""
import itk
import vtk

import connect2
import ImageCompare

import JT
import Calibration

def Test():
    """
    Test image difference function updated from DRR.

    An observer is added to look for the keypress 'u' to update the differnce image.
    """

    inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
    staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
    calFileLat = 'C:/Users/bryan/bryan-code/trunk/Tests/ext_cal1.txt'
    #drrImage = None

    # These should be normalized images
    fixedImageName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mha'
    image1Name = 'C:/Users/bryan/bryan-code/trunk/Tests/Test_ImageOverlayFixed.mhd'
    image2Name = 'C:/Users/bryan/bryan-code/trunk/Tests/Test_ImageOverlayMoving.mhd'
    outPath = 'C:/Users/bryan/bryan-code/trunk/Tests/'

    #
    # Diff Image Components
    #%############
    reader1 = vtk.vtkMetaImageReader()
    reader2 = vtk.vtkMetaImageReader()

    itk_reader = itk.ImageFileReader.IF2.New()

    caster1 = vtk.vtkImageCast()
    caster2 = vtk.vtkImageCast()

    writer = vtk.vtkMetaImageWriter()

    imageMath = vtk.vtkImageMathematics()

    vtk2itk = connect2.vtk2itk()
    itk_writer = itk.ImageFileWriter.IUC2.New()

    itk2vtk = connect2.itk2vtk()
    itk_cast = itk.CastImageFilter.IUC2IF2.New()

    ic = JT.ImageCanvas()

    textActor = vtk.vtkTextActor()
    textActor.SetInput("null")
    tprop = textActor.GetTextProperty()
    tprop.SetColor(1,0,0)

    #
    # Generate Lateral Projection
    #%###########################
    xraycamLat = JT.XRayCamera()
    drrLat = JT.DRR()
    calLat = Calibration.ExternalCalibration()

    calLat.LoadConsolidatedCalibration(calFileLat)
    calLat.LoadStaFile(staFile)
    drrLat.SetXRayCamera(xraycamLat)
    drrLat.SetVolumeFilename(inputVolFileName)
    drrLat.SetBlackOnWhiteImage()


    #
    # Automatic calibration
    #%##########################
    xraycamLat.SetExternalCalibration(calLat)

    vtkMatrixLAT = calLat.ConvertNumpyToVTKArray(calLat._VolumeT)
    drrLat.SetVolumeUserMatrix(vtkMatrixLAT)
    drrLat.SetBlackOnWhiteImage()
    drrImage = drrLat.GetImage('vtk')

    #
    # Open Fixed Image
    #%##########################
    itk_reader.SetFileName(fixedImageName)
    itk_reader.Update()

    ic._SetImage(itk_reader.GetOutput())


    #
    # Update Callback Function
    #%##########################
    def Update(obj,event):
        keysym = drrLat._iren.GetKeySym()
        if keysym == 't':
            textActor.SetInput(str(drrLat._volume._otfThreshold))
            drrLat._renWin.Render()
        if keysym == 'u':
            drrLat.GenerateImage()
            drrImage = drrLat.GetImage('vtk')
            UpdateDiffImageWindow()
            print "Image Updated"


    def UpdateDiffImageWindow():
        drrLat._renWin.Render()


    drrLat._iren.AddObserver("KeyPressEvent",Update)
    drrLat._renWin.AddRenderer(ic._ren)
    drrLat._ren.SetViewport(0.0,0.0,0.5,1.0)
    ic._ren.SetViewport(0.5,0.0,1.0,1.0)
    ic._ren.AddActor2D(textActor)
    drrLat._renWin.SetSize(1024,512)

    #UpdateDiffImageWindow()
    drrLat.Render()



##    #Test for positive match
##    try:
##        #return 0 if they match, 1 otherwise.
##        assert ImageCompare.compare(inputFilename,itk_SaveName)==0
##        print "ITK Test Success!"
##    except:
##        print "ITK Test Failure!"
##    #Test for positive match
##    try:
##        #return 0 if they match, 1 otherwise.
##        assert ImageCompare.compare(inputFilename,SaveName)==0
##        print "VTK Test Success!"
##    except:
##        print "VTK Test Failure!"



if __name__ == "__main__":
    Test()